<?php
/*
 * Camelot PHP Tools
 * This example illustrates how to download a List Item Attachement
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 * Note! 
 * Set up settings.php before using this file
 */
error_reporting(E_COMPILE_ERROR);
ini_set('display_errors', '1');


// Load all required files for Camelot PHP Tools
require_once '../loader.php';
?>

<?php include 'base/head.php'; ?>

<h2>Example LIST Commands</h2>
<p class="comment">For the full select documentation please visit <a href="http://www.bendsoft.com/documentation/camelot-net-connector/latest/sql-statement-syntax/select/">http://www.bendsoft.com/documentation/camelot-net-connector/latest/sql-statement-syntax/select/</a></p>
<p>List commands can be seen as a form of helpers where you simply state the list 
    name and view name, not having to write any SQL. If there is any need to transform
the data output this should be done in the view settings in SharePoint.</p>

<form method="post">
    <div class="query">
        <label>List name</label> 
        <input type="text" name="list1" value="<?php echo ((!isset($_POST['list1'])) || !$_POST ) ? 'Tasks' : $_POST['list1'] ?>" style="width: 150px;" />
        <input type="submit" name="list1_submit" value="Submit" />
    </div>

    <div class="query">
        <label>List name</label> 
        <input type="text" name="list2" value="<?php echo ((!isset($_POST['list2'])) || !$_POST ) ? 'Tasks' : $_POST['list2'] ?>" style="width: 150px;" />
        <label>View name</label> 
        <input type="text" name="list2_view" value="<?php echo ((!isset($_POST['list2_view'])) || !$_POST ) ? 'Active Tasks' : $_POST['list2_view'] ?>" style="width: 150px;" />
        <input type="submit" name="list2_submit" value="Submit" />
    </div>

    <div class="query">
        <label>List name</label> 
        <input type="text" name="list3" value="<?php echo ((!isset($_POST['list3'])) || !$_POST ) ? 'Tasks' : $_POST['list3'] ?>" style="width: 150px;" />
        <label>View name</label> 
        <input type="text" name="list3_view" value="<?php echo ((!isset($_POST['list3_view'])) || !$_POST ) ? 'Active Tasks' : $_POST['list3_view'] ?>" style="width: 150px;" />
        <label>Columns</label> 
        <input type="text" name="list3_columns" value="<?php echo ((!isset($_POST['list3_columns'])) || !$_POST ) ? 'LinkTitle;DueDate;Status' : $_POST['list3_columns'] ?>" style="width: 200px;" />
        <input type="submit" name="list3_submit" value="Submit" />
    </div>


    <div>
        <label>Render as table
            <input type="checkbox" name="renderAsTable" <?php echo ((isset($_POST['renderAsTable'])) || !$_POST ) ? 'checked="checked"' : '' ?>  />
        </label>
        <p class="comment">Meta property, render object or graphical table</p>
    </div>
</form>


<?php
if ($_POST) {

    // Stopwatch
    $time_pre = microtime(true);

    try {

        $list = '';
        $view = '';
        $columns = '';

        if (isset($_POST['list1_submit'])) {
            $list = $_POST['list1'];
        } elseif (isset($_POST['list2_submit'])) {
            $list = $_POST['list2'];
            $view = $_POST['list2_view'];
        } elseif (isset($_POST['list3_submit'])) {
            $list = $_POST['list3'];
            $view = $_POST['list3_view'];
            $columns = $_POST['list3_columns'];
        }


        // Build command
        $command = new SharePointListCommand();
        $command->connection_name = "SharePointConnection1";
        $command->listName = $list;
        $command->viewName = $view;
        $command->columns = $columns;

        // Execute command
        $result = new SharePointQuery($command);
        
        // If some form of exception occured and SILENT_EXCEPTIONS is set to TRUE
        if (!$result->commandStatus) {
            echo "<p>Silent exceptions is ON, this error message is forced to appear in this example</p>";
            $message = $result->exception->message;
            echo "<h2>Exception: $message</h2>";
            pr($result);
        } else {

            // Render result
            if (isset($_POST['renderAsTable'])) {

                // Load twig
                require_once '../twig/lib/Twig/Autoloader.php';
                Twig_Autoloader::register();
                $loader = new Twig_Loader_Filesystem('../templates');
                $twig = new Twig_Environment($loader);
                $template = $twig->loadTemplate('sp2007.html');

                echo $template->render(array(
                    'list_header' => $result->CamelotSoap->_sorted->_schema->display_name,
                    'list_content' => $result->CamelotSoap->_sorted->_content
                ));
            } else {
                // No fancy stuff, just dump the object
                pr($result);
            }
        }
    } catch (CamelotException $exc) {
        echo "<h2>Exception: $exc->message</h2>";
        pr($exc);
    }

    // ...end stopwatch
    $time_post = microtime(true);
    $exec_time = $time_post - $time_pre;
    echo "<p class=\"comment\">Query executed and data transfered in $exec_time seconds</p>";
}
?>

<?php include 'base/foot.html'; ?>